package com.evil.application.pojo.dto.view;

import com.alibaba.fastjson.JSON;
import com.evil.application.pojo.dto.view.config.OtherConfigDTO;
import com.evil.application.pojo.dto.view.config.ViewConfigDTO;
import com.evil.application.pojo.dto.view.position.PositionConfigDTO;
import com.evil.application.pojo.entity.ApplicationView;
import com.evil.common.application.enums.view.ApplicationViewTypeEnum;
import com.evil.common.core.enums.RCodeEnum;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import javax.validation.Valid;

/**
 * 应用视图 实体
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Data
public class ApplicationViewDTO {

    /**
     * 视图key
     */
    private String viewKey;

    /**
     * 视图名称
     */
    private String viewName;

    /**
     * 视图标题
     */
    private String viewTitle;

    /**
     * 视图类型 {@link ApplicationViewTypeEnum}
     */
    private Integer viewType;

    /**
     * 视图配置
     */
    @Valid
    private ViewConfigDTO viewConfig;

    /**
     * 其他配置
     */
    @Valid
    private OtherConfigDTO otherConfig;

    /**
     * 位置配置（mobile，pc）
     */
    @Valid
    private PositionConfigDTO position;

    public ApplicationViewDTO() {
        this.viewConfig = new ViewConfigDTO();
        this.otherConfig = new OtherConfigDTO();
        this.position = new PositionConfigDTO();
    }

    public ApplicationViewDTO(ApplicationView applicationView) {
        this.viewKey = applicationView.getViewKey();
        this.viewName = applicationView.getViewName();
        this.viewTitle = applicationView.getViewTitle();
        this.viewType = applicationView.getViewType();
        this.otherConfig = new OtherConfigDTO();

        if (StringUtils.isNotBlank(applicationView.getViewConfig())) {
            this.viewConfig = JSON.parseObject(applicationView.getViewConfig(), ViewConfigDTO.class);
        }
        if (StringUtils.isNotBlank(applicationView.getPosition())) {
            this.position = JSON.parseObject(applicationView.getPosition(), PositionConfigDTO.class);
        }
    }

    public ApplicationView toApplicationView() {
        ApplicationView applicationView = new ApplicationView();

        this.copyProp(applicationView);
        return applicationView;
    }

    public void copyProp(ApplicationView applicationView) {
        applicationView.setViewName(this.viewName);
        applicationView.setViewTitle(this.viewTitle);
        applicationView.setViewType(this.viewType);
        applicationView.setViewConfig(JSON.toJSONString(this.viewConfig));
        applicationView.setPosition(JSON.toJSONString(this.position));
    }

    public void checkProp() {
        switch (ApplicationViewTypeEnum.isExistById(this.viewType)) {
            case GRID:
                Assert.notNull(this.getViewConfig().getGridConfig(), RCodeEnum.ERROR_PARAMETER.getMessage());
                break;
            case LINK:
                Assert.notNull(this.getViewConfig().getLinkConfig(), RCodeEnum.ERROR_PARAMETER.getMessage());
                break;
            case TEXT:
                Assert.notNull(this.getViewConfig().getTextConfig(), RCodeEnum.ERROR_PARAMETER.getMessage());
                break;
            case FILTER:
                Assert.notNull(this.getViewConfig().getFilterConfig(), RCodeEnum.ERROR_PARAMETER.getMessage());
                break;
            case CAROUSEL:
                Assert.notNull(this.getViewConfig().getCarouselConfig(), RCodeEnum.ERROR_PARAMETER.getMessage());
                break;
            case REPORTING:
                Assert.notNull(this.getViewConfig().getReportingConfig(), RCodeEnum.ERROR_PARAMETER.getMessage());
                break;
            default:
                throw ApplicationViewTypeEnum.getBusinessException();
        }
    }
}
